package com.bwie.service.impl;

import com.bwie.exception.BusinessException;
import com.bwie.mapper.EmergencyContactMapper;
import com.bwie.mapper.OwnerMapper;
import com.bwie.mapper.StoreMapper;
import com.bwie.pojo.Result;
import com.bwie.pojo.Store;
import com.bwie.service.StoreService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreMapper storeMapper ;

    @Autowired
    private OwnerMapper ownerMapper ;

    @Autowired
    private EmergencyContactMapper emergencyContactMapper ;

    /**
     * 列表
     * @param store
     * @return
     */
    @Override
    public Result<PageInfo<Store>> selectList(Store store) {
        PageHelper.startPage(store.getPageNum(),store.getPageSize());
        List<Store> list = storeMapper.selectList(store);
        PageInfo<Store> pageInfo = new PageInfo<>(list);
        return Result.success("查询成功",pageInfo);
    }

    /**
     * 逻辑删除
     * @param store
     */
    @Transactional
    @Override
    public void toDelete(Store store) {
        //删除商铺
        storeMapper.deleteById(store.getStoreId());
        //删除业主
        ownerMapper.deleteBySid(store.getStoreId());
        //删除紧急联系人
        emergencyContactMapper.deleteBySid(store.getStoreId());
    }

    /**
     * 查询是否有关联
     * @param sid
     */
    @Override
    public void selectOwner(Integer sid) {
        int count = storeMapper.selectCount(sid);
        if(count > 0){//有绑定
            throw new BusinessException("该商铺下存在关联数据,确认删除吗");
        }
    }

    /**
     * 商铺下拉框
     * @return
     */
    @Override
    public Result selectStoreList() {
        List<Store> list = storeMapper.selectStoreList();
        return Result.success("下拉框查询成功",list);
    }

    @Override
    public Result<Store> selectOneByNum(String storeNum) {
        Store store = storeMapper.selectOneByNum(storeNum);
        return Result.success("查询成功",store);
    }
}
